/*
/*
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.geergenbein.azw.model.transcription;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapKey;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
//import org.unicode.unihan.model.Unihan;

/**
 *
 * @author andrew
 */
@Entity
@Table(name = "toned_code_point", 
    uniqueConstraints = @UniqueConstraint(columnNames = 
    {"untoned_code_point_id","tone"}))
@NamedQueries({
    @NamedQuery(name = "findTonedCodePoint",
    query = "select tcp from TonedCodePoint tcp " +
    "where tcp.untonedCodePoint = :untonedCodePoint and tcp.tone = :tone"),
    
            /*
    @NamedQuery(name="findTonedCodePointByTranscriptionAndTone",
    query=
            "select " +
            "   tcp " +
            "from " +
            "   TonedCodePoint as tcp " +
            "       left join tcp.untonedCodePoint as ucp " +
            "       left join ucp.dialect as d, " +
            "   UntonedTranscription as ut " +
            "       left join ut.transcriptionSystem as ts " +
            "where " +
            "   ts.name = :transcriptionSystemName and " +
            "   ucp.id = ut.untonedCodePoint and " +
            "   d.name = :dialectName and " +
            "   tcp.tone = :tone and " +
            "   ut.transcription = :transcription"),*/
            
    @NamedQuery(name="findTonedCodePointByTranscriptionAndTone",
    query=
            "select " +
            "   tcp " +
            "from " +
            "   TonedCodePoint as tcp " +
            "       left join tcp.untonedCodePoint as ucp " +
            "       left join ucp.dialect as d, " +
            "   UntonedTranscription as ut " +
            "       left join ut.transcriptionSystem as ts " +
            "where " +
            "   ts = :transcriptionSystem and " +
            "   ucp.id = ut.untonedCodePoint and " +
            "   d = :dialect and " +
            "   tcp.tone = :tone and " +
            "   ut.transcription = :transcription")
})
public class TonedCodePoint implements Serializable {
    
    private static final long serialVersionUID = 1L;
    private Long id;
    private UntonedCodePoint untonedCodePoint;
    private Integer tone;
    private String name;
    private Map<TranscriptionSystem, TonedTranscription> transcriptions;
    //private List<Unihan> unihans;

    public TonedCodePoint() {
    }
    
    public TonedCodePoint(UntonedCodePoint ucp, Integer tone, String name) {
        this.untonedCodePoint = ucp;
        this.tone = tone;
        this.name = name;
    }
    
    public void setId(Long id) {
        this.id = id;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().
                append(untonedCodePoint).
                append(tone).
                hashCode();
    }

    @Override
    public boolean equals(Object object) {
        
        if (this == object) {
            return true;
        }
        
        if (!(object instanceof TonedCodePoint)) {
            return false;
        }
        
        TonedCodePoint other = (TonedCodePoint) object;
        
        return new EqualsBuilder().
                append(untonedCodePoint, other.getUntonedCodePoint()).
                append(tone, other.getTone()).
                isEquals();
        
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).
                append(id).
                append(name).
                append(untonedCodePoint).
                append(tone).
                toString();
    }

    @ManyToOne
    @JoinColumn(name = "untoned_code_point_id", nullable = false)
    public UntonedCodePoint getUntonedCodePoint() {
        return untonedCodePoint;
    }

    public void setUntonedCodePoint(UntonedCodePoint untonedCodePoint) {
        this.untonedCodePoint = untonedCodePoint;
    }

    @Column(name = "tone", nullable = false)
    public Integer getTone() {
        return tone;
    }

    public void setTone(Integer tone) {
        this.tone = tone;
    }

    @Column(length=15, nullable=false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    @OneToMany(mappedBy = "tonedCodePoint")
    @MapKey(name = "transcriptionSystem")
    public Map<TranscriptionSystem, TonedTranscription> getTranscriptions() {
        return transcriptions;
    }

    public void setTranscriptions(Map<TranscriptionSystem, TonedTranscription> transcriptions) {
        this.transcriptions = transcriptions;
    }

}
